package test.socket.http;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpServer {

    public static void main(String[] args) throws InterruptedException {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();
            System.out.println("开始接收");
            String msg;
            byte[] bys = new byte[1024];
            int len = 0;
            InputStream inputStream = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream(new File("movie/2.mp3"));
            while ((len = inputStream.read(bys))!=-1){
                fos.write(bys);
                fos.flush();
              if(new String(bys,0,len).endsWith("end")){
                    break;
                }

            }
    /*        int length = inputStream.available();

            while (length>0){
                len = inputStream.read(bys2);
                fos.write(bys2,0,len);
                fos.flush();

                Thread.sleep(10);
                length = inputStream.available();
                System.out.println(length);
                }*/
            BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

            bos.write("上传成功".getBytes());
            bos.flush();
            //dos.writeUTF("上传成功");
            //dos.flush();
            System.out.println("接收完毕");
            Thread.sleep(50000);

      /*      StringBuilder stringBuilder = new StringBuilder();
            while ((msg = bufferedReader.readLine()).length()>0){
                stringBuilder.append(msg);
                stringBuilder.append("\r\n");
                if(msg.length()==0){
                    break;
                }
            }
            System.out.println(stringBuilder.toString());*/

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
